package com.zhu.rpc.client;

import com.zhu.rpc.client.handler.ClientHandler;
import com.zhu.rpc.client.handler.ClientHeartBeatHandler;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.handler.timeout.IdleStateHandler;

import java.util.concurrent.TimeUnit;

public class RpcClientChannelInitializer extends ChannelInitializer<Channel> {
    private final String String_Decode = "stringDecode";
    private final String Json_Decode = "jsonDecode";
    private final String String_Encode = "stringEncode";
    private final String Rpc_Handler = "rpcHandler";
    private String serverName;
    public RpcClientChannelInitializer() {
    }
    int port;
    String host;
    public RpcClientChannelInitializer(String serverName,int port,String host) {
        this.serverName = serverName;
        this.port = port;
        this.host = host;
    }

    @Override
    protected void initChannel(Channel ch) throws Exception {
        addRpcHandler(ch.pipeline());
    }

    private void addRpcHandler(ChannelPipeline pipeline) {
        pipeline.addLast("idleStateHandler", new IdleStateHandler(0, 20, 0, TimeUnit.SECONDS));
        pipeline.addLast("clientHeartBeatHandle", new ClientHeartBeatHandler(serverName,host,port));
//        pipeline.addLast(String_Decode,new StringDecoder());
        pipeline.addLast(String_Encode,new StringEncoder());
        pipeline.addLast(Rpc_Handler,new ClientHandler(serverName));
    }
}

